﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace $safeprojectname$
{
    /// <summary>
    ///     Extensions available on every place of the framework.
    /// </summary>
    public static partial class Ext
    {
        #region FormatWith
        /// <summary>
        ///     Calls string.Format(CultureInfo.CurrentCulture, value, args);
        ///     where the value is the template and args contains the array of arguments.
        ///     if value is null, string.Empty is returned
        /// </summary>
        /// <param name = "value">The value.</param>
        /// <param name = "args">The args.</param>
        /// <returns>formatted value, if value is null, string.Empty is returned</returns>
        public static string FormatWith(this string value, params object[] args)
        {
            if (value.IsEmpty())
            {
                return string.Empty;
            }
            return string.Format(CultureInfo.CurrentCulture, value, args);
        }
        #endregion FormatWith

        #region ToLength
        const string HorizontalEllipsis = "\u2026";

        /// <summary>
        ///     Cuts the string to provided length.
        ///     Null values are converted to string.Empty.
        /// </summary>
        /// <param name = "value">string to be adjusted. null will be converted int string.Empty</param>
        /// <param name = "length">target size for the longer values</param>
        /// <returns>adjusted string</returns>
        public static string ToLength(this string value, int length)
        {
            return value.ToLength(length, null);
        }

        /// <summary>
        ///     Cuts the string to provided length.
        ///     Null values are converted to string.Empty.
        ///     If there IsNotEmpty() appendix, it replaces the rest of the text.
        /// </summary>
        /// <param name = "value">string to be adjusted. null will be converted int string.Empty</param>
        /// <param name = "length">target size for the longer values</param>
        /// <param name = "appendixForLonger">if provided, replaces the longer part</param>
        /// <returns>adjusted string</returns>
        public static string ToLength(this string value, int length, string appendixForLonger)
        {
            if (value.IsEmpty())
            {
                return string.Empty;
            }

            var appendix = appendixForLonger ?? string.Empty;

            if (length >= value.Length)
            {
                return value;
            }

            return value.Substring(0, Math.Max(0, length - appendix.Length)) + appendix;
        }

        /// <summary>
        ///     Cuts the string to provided length.
        ///     Null values are converted to string.Empty.
        ///     useHEllip: true will replace the rest of the text with a special character '...' (\u2026)
        /// </summary>
        /// <param name = "value">string to be adjusted. null will be converted int string.Empty</param>
        /// <param name = "length">target size for the longer values</param>
        /// <param name = "useHorizontalEllipsis">true will replace the rest of the text with a character for '...' (\u2026)</param>
        /// <returns>adjusted string</returns>
        public static string ToLength(this string value, int length, bool useHorizontalEllipsis)
        {
            var suffix = useHorizontalEllipsis ? HorizontalEllipsis : null;
            return value.ToLength(length, suffix);
        }
        #endregion ToLength

        #region TrimEnd
        /// <summary>
        ///     If value ends with suffix, returned value is trimmed.
        ///     Else the value itself is returned. (StringComparison.InvariantCultureIgnoreCase is used)
        /// </summary>
        public static string TrimEnd(this string value, string suffix)
        {
            if (value.IsNull() || suffix.IsNull())
            {
                return value;
            }
            if (value.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase))
            {
                return value.Substring(0, value.Length - suffix.Length);
            }
            return value;
        }

        #endregion TrimEnd

        #region IsEqual string
        /// <summary>
        ///     IsEqual - Case insensitive string comparison.
        ///     Compares this 'value' with provided 'toBeCompared' string.
        ///     If 'value' or 'toBeCompared' is null:  returns false.
        ///     Else: returns value.Equals(toBeCompared, StringComparison.OrdinalIgnoreCase);
        /// </summary>
        /// <returns></returns>
        public static bool IsEqual(this string value, string toBeCompared)
        {
            if (value.IsNull() || toBeCompared.IsNull())
            {
                return false;
            }
            return value.Equals(toBeCompared, StringComparison.OrdinalIgnoreCase);
        }
        public static bool AnyIsEqual(this string value, params string[] toBeComparedColl)
        {
            if (value.IsNull() || toBeComparedColl.IsEmpty())
            {
                return false;
            }
            foreach (var toBeCompared in toBeComparedColl)
            {
                if (value.IsEqual(toBeCompared))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion IsEqual string

        #region ToSentence
        public static string ToSentence(this string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return string.Empty;
            }
            var newText = new StringBuilder(text.Length * 2);

            newText.Append(text[0]);

            var wasUpper = char.IsUpper(text[0]);
            for (int i = 1; i < text.Length; i++)
            {
                var appendSpace = char.IsUpper(text[i])
                    && !wasUpper
                    && text[i - 1] != ' ';

                if (appendSpace)
                {
                    newText.Append(' ');
                }
                newText.Append(text[i]);

                wasUpper = char.IsUpper(text[i]);
            }
            return newText.ToString();
        }
        #endregion ToSentence
    }
}